package controller;

import entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import service.StudentService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/")
public class StudentController {
    @Autowired
    StudentService studentService;

    // 学生退出
    @RequestMapping("/quit")
    public String quit(HttpServletRequest request,HttpSession session){
        session.setAttribute("student",null);
        return "index";
    }
    @RequestMapping("/login")
    public String login(HttpServletRequest request, Model model, HttpSession session) throws Exception{
        if(session.getAttribute("student")!=null){
            List<Student> allStudent = studentService.getAllStudent();
            model.addAttribute("studentList",allStudent);
            return "studentList";
        }
        request.setCharacterEncoding("utf-8");
        String stuId = request.getParameter("stuId");
        String pwd = request.getParameter("pwd");
        Student student = studentService.login(stuId, pwd);
        if(student!=null){
            System.out.println("登录成功！"+stuId);
            session.setAttribute("student",student);
            List<Student> allStudent = studentService.getAllStudent();
            model.addAttribute("studentList",allStudent);
            return "studentList";
        }else{
            System.out.println("登录失败！");
            model.addAttribute("err","用户名或密码错误！");
            return "index";
        }
    }

    @RequestMapping("/verifyStuId")
    @ResponseBody // 把返回值直接放到页面的body中，而不是把返回值作为页面地址被Spring MVC处理，否则会认为返回的是页面"yes.jsp"或"no.jsp",导致报404找不到页面的错误
    public String verifyStuId(@RequestBody Map<String, String> map) throws Exception{
        String data;

        String stuId = map.get("stuId");
        // 根据学号获得一个学生对象
        Student stu = studentService.getStudentById(stuId);
        if (stu != null) {
            data = "yes";
        } else {
            data = "no";
        }

        return data;
    }

    @RequestMapping("/studentAdd")
    public String studentAdd(){
        return "studentAdd";
    }

    // 新增后保存学生信息
    @RequestMapping(value="/studentAddSave", method= RequestMethod.POST)
    public String studentAddSave(HttpServletRequest request, Model model) throws Exception {
        // 设置request的编码
        request.setCharacterEncoding("UTF-8");
        // 获取用户输入的信息
        String stuId = request.getParameter("stuId");
        String stuName = request.getParameter("stuName");
        String stuClass = request.getParameter("stuClass");
        String stuSpecial = request.getParameter("stuSpecial");
        String age = request.getParameter("stuAge");
        if (age == null || age.isEmpty()) {
            age = "0";
        }
        int stuAge = Integer.parseInt(age);
        String stuSex = request.getParameter("stuSex");
        String stuPassword = request.getParameter("stuPassword");

        // 创建学生对象
        Student student = new Student();
        student.setStuId(stuId);
        student.setStuName(stuName);
        student.setStuClass(stuClass);
        student.setStuSpecial(stuSpecial);
        student.setStuAge(stuAge);
        student.setStuSex(stuSex);
        student.setStuPassword(stuPassword);
        // 调用Service添加学生记录
        studentService.addStudent(student);

        // 查询所有的学生记录
        List<Student> studentList = studentService.getAllStudent();
        // 把所有的学生对象放入模型中
        model.addAttribute("studentList", studentList);
        // 跳转到student-list.jsp页面
        return "redirect:login";
    }

    // 更新学生信息
    @RequestMapping(value="/studentUpdate")
    public String studentUpdate(HttpServletRequest request, Model model) throws Exception {
        // 获取传过来的参数
        String stuId = request.getParameter("stuId");
        // 根据学号获得一个学生的数据信息
        Student student = studentService.getStudentById(stuId);
        // 把学生对象放到model中
        model.addAttribute("student", student);
        // 跳转到student-update.jsp页面
        return "student-update";
    }

    // 更新后保存学生信息
    @RequestMapping(value="/studentUpdateSave")
    public String studentUpdateSave(HttpServletRequest request, Model model) throws Exception {
        // 设置request的编码
        request.setCharacterEncoding("UTF-8");
        // 获取用户输入的信息
        String stuId = request.getParameter("stuId");
        String stuName = request.getParameter("stuName");
        String stuClass = request.getParameter("stuClass");
        String stuSpecial = request.getParameter("stuSpecial");
        String age = request.getParameter("stuAge");
        if (age == null || age.isEmpty()) {
            age = "0";
        }
        int stuAge = Integer.parseInt(age);
        String stuSex = request.getParameter("stuSex");
        String stuPassword = request.getParameter("stuPassword");

        // 创建学生对象
        Student student = new Student();
        student.setStuId(stuId);
        student.setStuName(stuName);
        student.setStuClass(stuClass);
        student.setStuSpecial(stuSpecial);
        student.setStuAge(stuAge);
        student.setStuSex(stuSex);
        student.setStuPassword(stuPassword);
        // 通过Service执行修改操作
        studentService.updateStudent(student);

        // 查询所有的学生记录
        List<Student> studentList = studentService.getAllStudent();
        // 把所有的学生对象放入模型中
        model.addAttribute("studentList", studentList);
        // 跳转到student-list.jsp页面
        return "studentList";
    }

    // 删除学生信息
    @RequestMapping(value="/studentDelete")
    public String studentDelete(HttpServletRequest request, Model model) throws Exception {
        // 获取传过来的参数
        String stuId = request.getParameter("stuId");
        // 调用Service添加学生记录
        studentService.deleteStudentById(stuId);

        // 查询所有的学生记录
        List<Student> studentList = studentService.getAllStudent();
        // 把所有的学生对象放入模型中
        model.addAttribute("studentList", studentList);
        // 跳转到student-list.jsp页面
        return "redirect:login";
    }

}
